<?php

class express {

    function __construct(){
		global $tpl;
		$this->tpl = $tpl;
		$user = authenticate();
		if(isset($user['user']) && $user['user_id']==1){
			$tpl->assign('user',$user);
		}else{
			redirect(BASE_URL);
		}

	}
	function view_defaults(){
		echo "this is defaults ";
	}
	function view_addexpress(){
		$a_id = $_GET['a_id'];
		$user_id = $_GET['user_id'];

		$this->tpl->assign('a_id',$a_id);
		$this->tpl->assign('user_id',$user_id);
	}

	function op_addexpress(){
		$if_express = $_POST['if_express'];
		$a_id = isset($_POST['a_id']) && !empty($_POST['a_id'])?intval($_POST['a_id']):0;
		$user_id = isset($_POST['user_id']) && !empty($_POST['user_id'])?intval($_POST['user_id']):0;

		if($if_express){
			include_once 'ExpressModel.class.php';
			$expmod = new ExpressModel;

			$assigninfo = $expmod->getAssignmentInfoById($a_id);
			$applicantinfo = $expmod->getApplicantInfoById($user_id);

			$exp['a_id'] = $assigninfo['a_id'];
			$exp['a_title'] = $assigninfo['a_title'];
			$exp['a_sdate'] = $assigninfo['a_sdate'];
			$exp['a_edate'] = $assigninfo['a_edate'];

			$exp['user_id'] = $applicantinfo['user_id'];
			$exp['app_realname'] = $applicantinfo['realname'];
			$exp['app_mobile'] = $applicantinfo['mobile'];
			$exp['app_postaddress'] = $applicantinfo['postaddress'];

//			echo "<pre/>";
//			print_r($assigninfo);
//			print_r($applicantinfo);

			//更新该任务在assignment表中的快递状态
			include_once 'AssignmentModel.class.php';
			$assignmentmod = new AssignmentModel;

			$item['need_express'] = 1;//状态为1表示需要寄快递，否则为无需快递
			$up = $assignmentmod->updateAssignment($item, $a_id);

			$rs = $expmod->createNewExpress($exp);
			if($rs && $up){
				show_message('success');
				goback();
			}else{
				show_message('faild');
				goback();
			}
		}
	}

	function view_send(){
		//根据a_id找出任务名称，起止时间；根据user_id找出执行人姓名、手机、邮寄地址；将这些信息列表展现；
		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;

		$a_title = (isset($_GET['a_title']) && !empty($_GET['a_title'])) ? $_GET['a_title'] : '';
		$a_sdate = (isset($_GET['a_sdate']) && !empty($_GET['a_sdate'])) ? $_GET['a_sdate'] : '';
		$a_edate = (isset($_GET['a_edate']) && !empty($_GET['a_edate'])) ? $_GET['a_edate'] : '';

		$con = array();
		$con['a_title'] = $a_title;
		$con['a_sdate'] = $a_sdate;
		$con['a_edate'] = $a_edate;
		$con['is_sent'] = 0;

		$expresses = $expmod->getItems($con,'express',10);
		//$expresses = $expmod->getSendExpress();//where is_sent=0
		$total = $expresses['page']->total;

//		echo "<pre/>";
//		print_r($expresses);

		$this->tpl->assign('sexpress', $expresses['records']);
		$this->tpl->assign('total',$total);
		$this->tpl->assign('pagebar',$expresses['pagebar']);
		$this->tpl->assign('type','send');
		$this->tpl->assign('con',$con);

	}

	function view_sendexpress(){
		$a_id = isset($_GET['a_id'])?$_GET['a_id']:0;
		$user_id = isset($_GET['user_id'])?$_GET['user_id']:0;

		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;

		$assigninfo = $expmod->getAssignmentInfoById($a_id);
		$applicantinfo = $expmod->getApplicantInfoById($user_id);

		foreach($applicantinfo as $kk=>$vv){
			$assigninfo[$kk]=$vv;
		}

		$this->tpl->assign('express',$assigninfo);

	}

	function op_saveexpress(){
		//get the informations and update into database
		$a_id = (isset($_POST['a_id'])&& !empty($_POST['a_id']))?$_POST['a_id']:0;
		$a_title = (isset($_POST['a_title'])&& !empty($_POST['a_title']))?addslashes($_POST['a_title']):'';
		$a_sdate = (isset($_POST['a_sdate'])&& !empty($_POST['a_sdate']))?$_POST['a_sdate']:'0000-00-00';
		$a_edate = (isset($_POST['a_edate'])&& !empty($_POST['a_edate']))?$_POST['a_edate']:'0000-00-00';
		$app_realname = (isset($_POST['app_realname'])&& !empty($_POST['app_realname']))?addslashes($_POST['app_realname']):'';
		$app_mobile = (isset($_POST['app_mobile'])&& !empty($_POST['app_mobile']))?addslashes($_POST['app_mobile']):'';
		$app_postaddress = (isset($_POST['app_postaddress'])&& !empty($_POST['app_postaddress']))?addslashes($_POST['app_postaddress']):'';
		$exp_id = (isset($_POST['exp_id'])&& !empty($_POST['exp_id']))?addslashes($_POST['exp_id']):'';
		$exp_cost = (isset($_POST['exp_cost'])&& !empty($_POST['exp_cost']))?floatval($_POST['exp_cost']):0.00;
		$exp_note = (isset($_POST['exp_note'])&& !empty($_POST['exp_note']))?addslashes($_POST['exp_note']):'';

		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;

		$item = array();
		$item['a_title'] = $a_title;
		$item['a_sdate'] = $a_sdate;
		$item['a_edate'] = $a_edate;
		$item['app_realname'] = $app_realname;
		$item['app_mobile'] = $app_mobile;
		$item['app_postaddress'] = $app_postaddress;
		$item['is_sent']=1;
		$item['exp_id']=$exp_id;
		$item['exp_cost']=$exp_cost;
		$item['exp_note']=$exp_note;

		$rs = $expmod->updateExpress($item,$a_id);
		if($rs){
			show_message_goback('success');
		}else{
			show_message_goback('faild');
		}

	}

	function view_track(){
		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;

		$a_title = (isset($_GET['a_title']) && !empty($_GET['a_title'])) ? $_GET['a_title'] : '';
		$a_sdate = (isset($_GET['a_sdate']) && !empty($_GET['a_sdate'])) ? $_GET['a_sdate'] : '';
		$a_edate = (isset($_GET['a_edate']) && !empty($_GET['a_edate'])) ? $_GET['a_edate'] : '';
		$app_realname = (isset($_GET['app_realname']) && !empty($_GET['app_realname'])) ? $_GET['app_realname'] : '';

		$con = array();
		$con['a_title'] = $a_title;
		$con['a_sdate'] = $a_sdate;
		$con['a_edate'] = $a_edate;
		$con['app_realname'] = $app_realname;
		$con['is_sent'] = 1;
		$con['is_signed'] = 0;

		$sentexpress = $expmod->getItems($con, 'express',10);
		//$sentexpress = $expmod->getSentExpress();//where is_sent=1 and is_signed=0
		$total = $sentexpress['page']->total;

//		echo "<pre/>";
//		print_r($sentexpress);

		$this->tpl->assign('sentexpress',$sentexpress['records']);
		$this->tpl->assign('total',$total);
		$this->tpl->assign('pagebar',$sentexpress['pagebar']);
		$this->tpl->assign('type','track');
		$this->tpl->assign('con',$con);
	}

	function view_ifsign(){
		$ex_id = $_GET['ex_id'];

		$this->tpl->assign('ex_id',$ex_id);
	}

	function op_ifsign(){
		$ex_id = $_POST['ex_id'];

		$is_signed = $_POST['if_signed'];

		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;
		$item = array();
		if($is_signed){//如果确认签收，则更改数据库中的状态
			$item['is_signed']=1;
			$rs = $expmod->updateSigned($item,$ex_id);

			if($rs) show_message_goback('success');
			else show_message_goback('faild');
		}
	}

	function view_signed(){
		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;

		$a_title = (isset($_GET['a_title']) && !empty($_GET['a_title'])) ? $_GET['a_title'] : '';
		$a_sdate = (isset($_GET['a_sdate']) && !empty($_GET['a_sdate'])) ? $_GET['a_sdate'] : '';
		$a_edate = (isset($_GET['a_edate']) && !empty($_GET['a_edate'])) ? $_GET['a_edate'] : '';
		$app_realname = (isset($_GET['app_realname']) && !empty($_GET['app_realname'])) ? $_GET['app_realname'] : '';

		$con = array();
		$con['a_title'] = $a_title;
		$con['a_sdate'] = $a_sdate;
		$con['a_edate'] = $a_edate;
		$con['app_realname'] = $app_realname;
		$con['is_signed'] = 1;

		$signedexpress = $expmod->getItems($con, 'express', 10);
		//$signedexpress = $expmod->getSignedExpress();//where is_signed=1
		$total = $signedexpress['page']->total;

		$this->tpl->assign('type','signed');
		$this->tpl->assign('total',$total);
		$this->tpl->assign('signedexpress',$signedexpress['records']);
		$this->tpl->assign('pagebar',$signedexpress['pagebar']);
		$this->tpl->assign('con',$con);
	}

	//快递接收
	function view_receive(){//添加分页栏
		//需接收的快递应为已签收了快递的任务
		//接收快递中应分为：待接收快递receive和已接收received的快递；

		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;

		$a_title = (isset($_GET['a_title']) && !empty($_GET['a_title'])) ? $_GET['a_title'] : '';
		$a_sdate = (isset($_GET['a_sdate']) && !empty($_GET['a_sdate'])) ? $_GET['a_sdate'] : '';
		$a_edate = (isset($_GET['a_edate']) && !empty($_GET['a_edate'])) ? $_GET['a_edate'] : '';
		$app_realname = (isset($_GET['app_realname']) && !empty($_GET['app_realname'])) ? $_GET['app_realname'] : '';

		$con = array();
		$con['a_title'] = $a_title;
		$con['a_sdate'] = $a_sdate;
		$con['a_edate'] = $a_edate;
		$con['app_realname'] = $app_realname;
		$con['is_signed'] = 1;
		$con['is_received'] = 0;

		$signedexpress = $expmod->getItems($con, 'express', 10);
		//$signedexpress = $expmod->getReceiveExpress();//where is_signed=1 and is_received=0
		$total = $signedexpress['page']->total;

		$this->tpl->assign('signedexpress', $signedexpress['records']);
		$this->tpl->assign('total',$total);
		$this->tpl->assign('pagebar',$signedexpress['pagebar']);
		$this->tpl->assign('type','receive');
		$this->tpl->assign('con',$con);
	}

	function view_receivingexpress(){
		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;

		$ex_id = (isset($_REQUEST['ex_id'])&& !empty($_REQUEST['ex_id']))?$_REQUEST['ex_id']:0;
		$express = $expmod->getExpressById($ex_id);

		$this->tpl->assign('express', $express);
	}

	function op_receivingexpress(){
		$ex_id = (isset($_REQUEST['ex_id']) && !empty($_REQUEST['ex_id']))?$_REQUEST['ex_id']:0;
		$recv_name = (isset($_REQUEST['recv_name']) && !empty($_REQUEST['recv_name']))?addslashes($_REQUEST['recv_name']):'';
		$recv_cost = (isset($_REQUEST['recv_cost']) && !empty($_REQUEST['recv_cost']))?addslashes($_REQUEST['recv_cost']):'';
		$recv_extend = (isset($_REQUEST['recv_extend']) && !empty($_REQUEST['recv_extend']))?addslashes($_REQUEST['recv_extend']):'';

		$item = array();
		$item['recv_name'] = $recv_name;
		$item['recv_cost'] = $recv_cost;
		$item['recv_extend'] = $recv_extend;
		$item['is_received'] = 1;

		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;

		$rs = $expmod->updateSigned($item, $ex_id);

		if($rs) show_message_goback('success');
			else show_message_goback('faild');

	}

	function view_received(){
		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;

		$a_title = (isset($_GET['a_title']) && !empty($_GET['a_title'])) ? $_GET['a_title'] : '';
		$a_sdate = (isset($_GET['a_sdate']) && !empty($_GET['a_sdate'])) ? $_GET['a_sdate'] : '';
		$a_edate = (isset($_GET['a_edate']) && !empty($_GET['a_edate'])) ? $_GET['a_edate'] : '';
		$app_realname = (isset($_GET['app_realname']) && !empty($_GET['app_realname'])) ? $_GET['app_realname'] : '';

		$con = array();
		$con['a_title'] = $a_title;
		$con['a_sdate'] = $a_sdate;
		$con['a_edate'] = $a_edate;
		$con['app_realname'] = $app_realname;
		$con['is_signed'] = 1;
		$con['is_received'] = 1;

		$recvdexpress = $expmod->getItems($con, 'express', 10);
		//$recvdexpress = $expmod->getReceivedExpress();//where is_signed=1 and is_received=1
		$total = $recvdexpress['page']->total;

		$this->tpl->assign('recvdexpress', $recvdexpress['records']);
		$this->tpl->assign('total',$total);
		$this->tpl->assign('pagebar',$recvdexpress['pagebar']);
		$this->tpl->assign('type','received');
		$this->tpl->assign('con',$con);
	}

	//导出报表
	function view_toreport(){
		$a_sdate = (isset($_GET['a_sdate']) && !empty($_GET['a_sdate'])) ? $_GET['a_sdate'] : '';
		$a_edate = (isset($_GET['a_edate']) && !empty($_GET['a_edate'])) ? $_GET['a_edate'] : '';

		//先将express表中的所有信息取出来；
		//必要的信息：任务名称，执行时间，神秘顾客姓名、邮寄地址、联系方式，快递订单号，消费卡号，快递是否已签收，寄出费用，快递到付确认，到付费用，补汇金额
		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;
		$con = array();
		$con['a_sdate'] = $a_sdate;
		$con['a_edate'] = $a_edate;
		$allexpress = $expmod->getItems($con,'express',10);//得到所有的快递
		$total = $allexpress['page']->total;

//		echo "<pre/>";
//		print_r($express);

		//显示快递列表
		$this->tpl->assign('allexpress',$allexpress['records']);
		$this->tpl->assign('con',$con);
		$this->tpl->assign('pagebar',$allexpress['pagebar']);
		$this->tpl->assign('total',$total);
	}

	function view_toexcel(){
		$a_sdate = (isset($_GET['a_sdate']) && !empty($_GET['a_sdate'])) ? $_GET['a_sdate'] : '';
		$a_edate = (isset($_GET['a_edate']) && !empty($_GET['a_edate'])) ? $_GET['a_edate'] : '';

		//重新筛选信息
		include_once 'ExpressModel.class.php';
		$expmod = new ExpressModel;
		$con = array();
		$con['a_sdate'] = $a_sdate;
		$con['a_edate'] = $a_edate;
		$express = $expmod->getItemsWithNopage($con,'express');//得到所有的快递

		//导出为本地excel
		/** 导出为EXCEL start **/

		/** Error reporting */
		error_reporting(E_ALL);

		//date_default_timezone_set('Europe/London');

		/** PHPExcel */
		require_once 'plugins/phpexcel/Classes/PHPExcel.php';


		// Create new PHPExcel object
		$objPHPExcel = new PHPExcel();

		// Set properties
		$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
									 ->setLastModifiedBy("Maarten Balliauw")
									 ->setTitle("Office 2007 XLSX Test Document")
									 ->setSubject("Office 2007 XLSX Test Document")
									 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
									 ->setKeywords("office 2007 openxml php")
									 ->setCategory("Test result file");

		//设置标题
		$objPHPExcel->setActiveSheetIndex(0)
		            ->setCellValue('A1', '任务标题')
		            ->setCellValue('A2', '任务标题')
		            ->setCellValue('B2', '开始时间')
		            ->setCellValue('C2', '结束时间')
		            ->setCellValue('D2', '姓名')
		            ->setCellValue('E2', '联系方式')
		            ->setCellValue('F2', '通信地址')
		            ->setCellValue('G2', '快递单号(寄出)')
		            ->setCellValue('H2', '消费卡号')
		            ->setCellValue('I2', '签收')
		            ->setCellValue('J2', '到付快递')
		            ->setCellValue('K2', '到付费用')
		            ->setCellValue('L2', '快递费(寄出)')
		            ->setCellValue('M2', '补汇金额');


		//设置表格内容

		if(is_array($express) && !empty($express)){
			for($i=0,$j=3;$i<count($express); $i++){
				$v = $express[$i];

				$objPHPExcel->setActiveSheetIndex(0)
							->setCellValue('A'.$j, $v['a_title'])
							->setCellValue('B'.$j, $v['a_sdate'])
							->setCellValue('C'.$j, $v['a_edate'])
							->setCellValue('D'.$j, $v['app_realname'])
							->setCellValue('E'.$j, $v['app_mobile'])
							->setCellValue('F'.$j, $v['app_postaddress'])
							->setCellValue('G'.$j, $v['exp_id'])
							->setCellValue('H'.$j, $v['exp_note'])
							->setCellValue('I'.$j, $v['is_signed'])
							->setCellValue('J'.$j, $v['recv_name'])
							->setCellValue('K'.$j, $v['recv_cost'])
							->setCellValue('L'.$j, $v['exp_cost'])
							->setCellValue('M'.$j, $v['recv_extend']);
				$j++;
			}
		}
		$objPHPExcel->setActiveSheetIndex(0);

		// Rename sheet
		$objPHPExcel->getActiveSheet()->setTitle('a_title');


		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);


		// Redirect output to a client’s web browser (Excel2007)
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="applicant.xlsx"');
		header('Cache-Control: max-age=0');

		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save('php://output');
		exit;
		/** 导出为EXCEL end **/

	}
}
?>